package basic_1028;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		Map<String, Birth> map=new HashMap<String, Birth>();
		List<Entry<String, Birth>> list=new ArrayList<Entry<String, Birth>>();
		
		Scanner scanner=new Scanner(System.in);
		int N=scanner.nextInt();
		
		int valueCount=0;
		
		for(int i=0;i<N;i++) {
			String name=scanner.next();
			String birthString=scanner.next();
			String[] births=birthString.split("/");
			int year=Integer.parseInt(births[0]);
			int month=Integer.parseInt(births[1]);
			int date=Integer.parseInt(births[2]);
			
			int value=year*365+month*31+date;
			if(value>1814*365+9*31+5&&value<2014*365+9*31+7) {
				Birth birth=new Birth(year, month, date);
				map.put(name, birth);
				valueCount++;
			}
		}
		scanner.close();
		
		list.addAll(map.entrySet());
		
		Collections.sort(list, new Comparator<Entry<String, Birth>>() {
			@Override
			public int compare(Entry<String, Birth> b1, Entry<String, Birth> b2) {
				if(b1.getValue().year*365+b1.getValue().month*31+b1.getValue().date>b2.getValue().year*365+b2.getValue().month*31+b2.getValue().date) {
					return 1;
				}
				return -1;
			}
		});
		
		System.out.println(""+valueCount+" "+list.get(0).getKey()+" "+list.get(list.size()-1).getKey());
	}
	
	static class Birth{
		int year,month,date;
		Birth(int year,int month,int date){
			this.year=year;
			this.month=month;
			this.date=date;
		}
	}
}
